package com.xique.community.controller;

import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.community.bean.domain.User;
import com.xique.community.biz.service.IUserBizService;
import com.xique.community.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 门禁用户信息Controller
 *
 * @author caogq
 * @date 2021-09-18
 */
@RestController
@RequestMapping("/user")
public class CommunityUserController extends BaseController
{
    @Autowired
    private IUserService userService;

    @Autowired
    private IUserBizService userBizService;

    /**
     * 获取门禁用户信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return userBizService.getUserInfo(id);
    }

    @GetMapping(value = "/list")
    public AjaxResult getUserList(@Param("userIds") List<Long> userIds) {
        return userBizService.getUserList(userIds);
    }

    /**
     * 查询门禁用户信息列表
     */
    @PreAuthorize(hasPermi = "community:user:list")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出门禁用户信息列表
     */
    @PreAuthorize(hasPermi = "community:user:export")
    @Log(title = "门禁用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) throws IOException
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "门禁用户信息数据");
    }

    /**
     * 新增门禁用户信息
     */
    @PreAuthorize(hasPermi = "community:user:add")
    @Log(title = "门禁用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改门禁用户信息
     */
    @PreAuthorize(hasPermi = "community:user:edit")
    @Log(title = "门禁用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除门禁用户信息
     */
    @PreAuthorize(hasPermi = "community:user:remove")
    @Log(title = "门禁用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
